package com.jf.StudenManage.controller;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class ImgServlet   extends   HttpServlet{
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//1.先获取  BufferedImage对象  
		BufferedImage  bfi=new  BufferedImage(68,28,BufferedImage.TYPE_INT_RGB);
		//2.获取画笔
		 Graphics   g=bfi.getGraphics();
		//3.画出一个长方形背景框出来
		 Color  color=new Color(200,215,250);
		 g.setColor(color);
		 g.fillRect(0, 0, 68,30 );
		 //4.创建随机数类
		 Random  r=new  Random();
		//5.生成第一个随机数
	     int    no1=r.nextInt(10);
	     g.setColor(new  Color(r.nextInt(100),r.nextInt(100),r.nextInt(100)));
		 g.drawString(no1+"",2,18);
		//6.画符号
		  
	     g.setColor(new  Color(r.nextInt(100),r.nextInt(100),r.nextInt(100)));
		 g.drawString("+",18,18);
		//7.下一个随机数 
		  int    no2=r.nextInt(10);
		  g.setColor(new  Color(r.nextInt(100),r.nextInt(100),r.nextInt(100)));
	      g.drawString(no2+"",30,18);
	    //8.画等于号
		  g.setColor(new  Color(r.nextInt(100),r.nextInt(100),r.nextInt(100)));
	      g.drawString("=",40,18);
		 //9.画问号
	      g.setColor(new  Color(r.nextInt(100),r.nextInt(100),r.nextInt(100)));
	      g.drawString("?",50,18);
		 
		  //将正确答案保存到 session对象
	      int   result=no1+no2;
	      HttpSession  session=request.getSession();
	      session.setAttribute("answer",result+"");		 
	      //通过  response获取到的 OutPutStream流  开始画出这张图片
	      ImageIO.write(bfi, "JPG",response.getOutputStream());
	}
      
}
